using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RapidHDL
{
    public partial class TextViewer : UserControl
    {
        public TextViewer()
        {
            InitializeComponent();
        }

        private void TextViewer_Load(object sender, EventArgs e)
        {

        }


        private void ColorVerilog(RapidHDL.ComponentVerilog poVerilog)
        {
            SetTextColor(Color.Violet);
            foreach (string sKeyword in poVerilog.ParentComponent.PassthroughNodeVectors.Keys)
            {
                ColorText(sKeyword);
            }

            SetTextColor(Color.Cyan);
            foreach (NodeVector oNodeVector in poVerilog.ParentComponent.InputNodeVectors)
            {
                string sKeyword = oNodeVector.Name;
                ColorText(sKeyword);
            }

            SetTextColor(Color.Orange);
            foreach (NodeVector oNodeVector in poVerilog.ParentComponent.OutputNodeVectors)
            {
                string sKeyword = oNodeVector.Name;
                ColorText(sKeyword);
            }

            SetTextColor(Color.Green);
            foreach (NodeVector oNodeVector in poVerilog.ParentComponent.RedundantNodeVectors)
            {
                string sKeyword = oNodeVector.Name;
                ColorText(sKeyword);
            }

            SetTextColor(Color.Red);
            foreach (RapidHDL.Component oComponent in poVerilog.ParentComponent.Components.Values)
            {
                if (oComponent.ComponentVerilog.VerilogModuleName != null)
                {
                    string sKeyword = oComponent.ComponentVerilog.VerilogModuleName.ToString();
                    ColorText(sKeyword);
                }
            }





            SetTextColor(Color.Blue);

            ColorText("always");

            ColorText("assign");

            ColorText("automatic");

            ColorText("begin");

            ColorText("case");

            ColorText("casex");

            ColorText("casez");

            ColorText("cell");

            ColorText("config");

            ColorText("deassign");

            ColorText("default");

            ColorText("defparam");

            ColorText("design");

            ColorText("disable");

            ColorText("edge");

            ColorText("else");

            ColorText("end");

            ColorText("endcase");

            ColorText("endconfig");

            ColorText("endgenerate");

            ColorText("endmodule");

            ColorText("endfunction");

            ColorText("endprimitive");

            ColorText("endspecify");

            ColorText("endtable");

            ColorText("endtask");

            ColorText("event");

            ColorText("for");

            ColorText("force");

            ColorText("forever");

            ColorText("fork");

            ColorText("function");

            ColorText("generate");

            ColorText("genvar");

            ColorText("if");

            ColorText("ifnone");

            ColorText("incdir");

            ColorText("include");

            ColorText("initial");

            ColorText("inout");


            ColorText("input");

            ColorText("instance");

            ColorText("join");

            ColorText("liblist");

            ColorText("library");

            ColorText("localparam");

            ColorText("macromodule");

            ColorText("module");

            ColorText("negedge");

            ColorText("noshowcancelled");

            ColorText("output");

            ColorText("parameter");

            ColorText("posedge");

            ColorText("primitive");

            ColorText("pulsestyle_ondetect");

            ColorText("pulsestyle_onevent");

            ColorText("reg");

            ColorText("release");

            ColorText("repeat");

            ColorText("scalared");

            ColorText("showcancelled");

            ColorText("signed");

            ColorText("specify");

            ColorText("specparam");

            ColorText("strength");

            ColorText("table");

            ColorText("task");

            ColorText("tri");

            ColorText("tri0");

            ColorText("tri1");

            ColorText("triand");

            ColorText("trior");

            ColorText("wand");

            ColorText("wor");

            ColorText("trireg");

            ColorText("unsigned");

            ColorText("use");

            ColorText("vectored");

            ColorText("wait");

            ColorText("while");

            ColorText("wire");



        }

        public bool ReadOnly
        {
            get { return rtbText.ReadOnly; }
            set { rtbText.ReadOnly = value; }
        }

        public bool LoadVerilog(ComponentVerilog poVerilog)
        {
            bool bLoaded = false;
            string sFileName = poVerilog.FileName;
            this.Text = sFileName;
            string sVerilog = "";
            try
            {
                System.IO.StreamReader srText = RapidHDL.TextFile.OpenFileRead(sFileName);
                sVerilog = srText.ReadToEnd();
                bLoaded = true;
            }
            catch (Exception e)
            {
                sVerilog = e.Message;
            }

            rtbText.Text = sVerilog;

            ColorVerilog(poVerilog);
            Application.DoEvents();
            return bLoaded;
        }


        public bool LoadLogFile(string sFileName)
        {
            bool bLoaded = false;
            this.Text = sFileName;
            string sLogText = "";
            try
            {
                System.IO.StreamReader srText = RapidHDL.TextFile.OpenFileRead(sFileName);
                sLogText = srText.ReadToEnd();
                bLoaded = true;
            }
            catch (Exception e)
            {
                sLogText = e.Message;
            }

            rtbText.Text = sLogText;

            SetupLogHighlighting();
            Application.DoEvents();
            return bLoaded;
        }


        private void SetupLogHighlighting()
        {
            /*SetTextColor(Color.Red);
            ColorText("ERROR");
            SetTextColor(Color.Orange);
            ColorText("WARNING");*/ 
        }

        private Color oColor;


        private void SetTextColor(Color poColor)
        {
            oColor = poColor;
        }

        private void ColorText(string sWord)
        {
            int iPosition;
            int iStart = 0;
            string sSearch = sWord;

            iPosition = rtbText.Find(sSearch, iStart, RichTextBoxFinds.WholeWord | RichTextBoxFinds.MatchCase);
            while (iPosition > 0)
            {
                rtbText.Select(iPosition, sSearch.Length);
                rtbText.SelectionColor = oColor;
                iStart = iPosition + 1;
                iPosition = rtbText.Find(sSearch, iStart, RichTextBoxFinds.WholeWord | RichTextBoxFinds.MatchCase);
            }
        }

        public void ShowTranscript(string psTranscript)
        {
            rtbText.Text = psTranscript;

            SetTextColor(Color.Red);
            ColorText("ERROR");
            SetTextColor(Color.Orange);
            ColorText("WARNING");
            SetTextColor(Color.Green);
            ColorText("Starting Hardware Script");
            SetTextColor(Color.Blue);
            ColorText("Completed Script");
            SetTextColor(Color.Purple);
            ColorText("PAUSE");

            rtbText.SelectionStart = rtbText.TextLength;
            rtbText.ScrollToCaret();
        }

        private void rtbText_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
